TARGET = lvgl_bin
STATIC_LIB_FULL_NAME = liblvgl.a


PROJECT_PATH = $(shell pwd)
LVGL_PATH ?= $(PROJECT_PATH)/modules/lvgl
include $(LVGL_PATH)/lvgl.mk

FATFSEMU_PATH = $(PROJECT_PATH)/modules/FileSystem
include $(FATFSEMU_PATH)/fatfs.mk

INCLUDE += "-I$(PROJECT_PATH)" "-I$(PROJECT_PATH)/porting" "-I$(PROJECT_PATH)/user"

MACROS += -DMY_DISP_HOR_RES=480 -DMY_DISP_VER_RES=600
MACROS += -DLV_CONF_INCLUDE_SIMPLE -DLV_LVGL_H_INCLUDE_SIMPLE
MACROS += -DLV_FS_NATIVE_LETTER_STR='"N:"' -DLV_FS_FATFS_LETTER_STR='"F:"'

ifneq ($(EN_MOUSE),)
FLAGS += -DEN_MOUSE
endif

ifneq ($(DEBUG),)
FLAGS += -g
MACROS += -DDEBUG
endif

AOBJS = $(patsubst %.S, %.o, $(ASRCS))
COBJS = $(patsubst %.c, %.o, $(CSRCS))
CXXOBJS = $(patsubst %$(CXXEXT), %.o, $(CXXSRCS))

MAINSRCS = $(wildcard *.c)
MAINSRCS += $(shell find $(PROJECT_PATH)/user -type f -name '*.c')
MAINSRCS += $(shell find $(PROJECT_PATH)/porting/ -type f -name '*.c')
MAINOBJS = $(patsubst %.c, %.o, $(MAINSRCS))


AFLAGS += $(INCLUDE) $(MACROS) $(FLAGS)
CFLAGS += $(INCLUDE) $(MACROS) $(FLAGS) -std=c99
CXXFLAGS += $(INCLUDE) $(MACROS) $(FLAGS) -std=c++11

LDFLAGS += -L. -llvgl -lm -lSDL2

all: lib $(TARGET)

$(TARGET): $(STATIC_LIB_FULL_NAME) $(MAINOBJS)
	$(CC) -o $@ $(CFLAGS) $(MAINOBJS) $(LDFLAGS)

lib: $(STATIC_LIB_FULL_NAME)

$(STATIC_LIB_FULL_NAME): $(AOBJS) $(COBJS) $(CXXOBJS)
	$(AR) rcs $@ $^

%.o: %.S
	$(CC) -o $@ -c $(AFLAGS) $<

%.o: %.c
	$(CC) -o $@ -c $(CFLAGS) $<

%.o: %$(CXXEXT)
	$(CC) -o $@ -c $(CXXFLAGS) $<


clean:
	-$(RM) $(STATIC_LIB_FULL_NAME) $(AOBJS) $(COBJS) $(CXXOBJS) $(MAINOBJS) $(TARGET)

distclean: clean
	-$(RM) flash_emu_dev_file1.img tags types_*.taghl

run: $(TARGET)
	./$(TARGET)

.PHONY: lib clean distclean run
